package com.atguigu.day09;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

public class Flink10_HiveCataLog {
    public static void main(String[] args) {

        //设置用户权限
        System.setProperty("HADOOP_USER_NAME", "atguigu");

        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        String name = "MyHive";
        String defaultDatabase = "flink_test";
        String hiveConfigDir = "c:/conf";

        //3.创建HiveCatalog
        HiveCatalog hiveCatalog = new HiveCatalog(name, defaultDatabase, hiveConfigDir);

        //4.注册HiveCatalog
        tableEnv.registerCatalog(name, hiveCatalog);

        //5.设置使用哪个catalog
        tableEnv.useCatalog(name);

        //6.设置使用哪个数据库
        tableEnv.useDatabase(defaultDatabase);

        //7.通过sql查询hive中的表数据
        tableEnv.executeSql("select * from stu").print();

    }
}
